package cc.flyflow.biz.process.service.impl;

import cc.flyflow.biz.process.entity.ProcessInstanceRecord;
import cc.flyflow.biz.process.mapper.ProcessInstanceRecordMapper;
import cc.flyflow.biz.process.service.IClearService;
import cc.flyflow.biz.process.service.IProcessInstanceRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程记录 服务实现类
 *
 * @author Vincent
 * @since 2023-05-07
 */
@Service
public class ProcessInstanceRecordServiceImpl extends ServiceImpl<ProcessInstanceRecordMapper, ProcessInstanceRecord> implements IProcessInstanceRecordService, IClearService
{

    /**
     * 根据流程实例id查询流程记录
     *
     * @param processInstanceId 流程实例id
     * @return 流程实例
     */
    @Override
    public ProcessInstanceRecord getByProcessInstanceId(String processInstanceId)
    {
        return this.lambdaQuery().eq(ProcessInstanceRecord::getProcessInstanceId, processInstanceId).one();
    }

    /**
     * 获取 流程实例主键集合
     *
     * @param businessCode 业务系统编码
     * @return processIds
     */
    @Override
    public List<String> getByBusinessCode(String businessCode)
    {
        List<ProcessInstanceRecord> list = this.lambdaQuery().like(ProcessInstanceRecord::getBusinessCode,
                businessCode).list();
        List<String> processId = new ArrayList<>();
        for (ProcessInstanceRecord record : list)
        {
            processId.add(record.getProcessInstanceId());
        }
        return processId;
    }

    /**
     * 获取 流程实例主键集合
     *
     * @param processBizCode 流程系统编码
     * @return processIds
     */
    @Override
    public List<String> getByProcessBizCode(String processBizCode)
    {
        List<ProcessInstanceRecord> list = this.lambdaQuery().like(ProcessInstanceRecord::getProcessInstanceBizCode,
                processBizCode).list();
        List<String> processId = new ArrayList<>();
        for (ProcessInstanceRecord record : list)
        {
            processId.add(record.getProcessInstanceId());
        }
        return processId;
    }

    /**
     * 清理数据
     *
     * @param uniqueId      流程唯一id
     * @param flowIdList    process表 流程id集合
     * @param processIdList process表的注解id集合
     * @param tenantId      租户id
     */
    @Override
    public void clearProcess(String uniqueId, List<String> flowIdList, List<Long> processIdList, String tenantId)
    {
        this.lambdaUpdate()
                .in(ProcessInstanceRecord::getFlowId, flowIdList)
                .eq(ProcessInstanceRecord::getTenantId, tenantId)
                .remove();
    }

}
